package com.hzqy.web.ac;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.service.behaviorControl.BehaviorControlServiceBean;
import com.hzqy.service.user.UserServiceBean;
import com.hzqy.web.vo.behaviorControl.IndustryGroupSchedulesVo;
import com.hzqy.web.vo.behaviorControl.IndustryOrgContentSchedulesVo;
import com.hzqy.web.vo.behaviorControl.IndustrySchedulesVo;

@Controller
@RequestMapping("/industry/user")
public class UserAction {

	protected final Logger LOG = LoggerFactory.getLogger(getClass()); 
	
	@Autowired
	private UserServiceBean userServiceBean;
	
	
	/**
	 * 新增用户与组织，角色关系
	 * @param
	 * @return
	 */
	@RequestMapping("/add_user_org_roles")
	@ResponseBody
	public Object addUserOrgRoles(PmsUserVo pmsUserVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = userServiceBean.addUserOrgRoles(pmsUserVo);
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","新增成功!");
		} else {
			resultMap.put("success", false);
			resultMap.put("result","新增失败!flag="+flag);
		}
		return resultMap;
	}

	
	@RequestMapping("/select_user")
	@ResponseBody
	public Object selectUserList(HttpSession session,PmsUserVo pmsUserVo, int page,int rows) {
		Map<String,Object> userListMap = userServiceBean.selectUserList(session,pmsUserVo,page,rows);
		return userListMap;
	}
	
	/*@RequestMapping("/select_user_byid")
	@ResponseBody
	public Object selectUserByid(HttpSession session,PmsUserVo pmsUserVo, int page,int rows) {
		Map<String,Object> userListMap = userServiceBean.selectUserList(session,pmsUserVo,page,rows);
		return userListMap;
	}*/
	
	@RequestMapping("/user_edit")
	@ResponseBody
	public Object userEdit(ModelMap map, PmsUserVo puv){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int count = userServiceBean.findUserIsExist(puv);
		if (count>0) {
			resultMap.put("success", false);
			resultMap.put("result","用户名已存在!");
		}else {
			int flag = userServiceBean.updateUser(puv);
			if(flag>0) {
				resultMap.put("success", true);
				resultMap.put("result","修改成功!");
			} else {
				resultMap.put("success", false);
				resultMap.put("result","修改用户记录失败!flag="+flag);
			}
		}
		return resultMap;
	}
	
	@RequestMapping("/orgRole_select")
	@ResponseBody
	public Object orgRoleSelect(HttpSession session){
		List<PmsOrgRoleVo> porvList = new ArrayList<PmsOrgRoleVo>();
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		porvList = userServiceBean.selectOrgRole(userVo);
		return porvList;
	}
	
	@RequestMapping("/user_show")
	@ResponseBody
	public Object userShow(HttpSession session,PmsUserVo puv){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		PmsUserVo pmsUserVo = userServiceBean.findUserById(session,puv);
		if(pmsUserVo==null){
			resultMap.put("result", "未拥有此权限！");
			return resultMap;
		}
		return pmsUserVo;
	}

	/**
	 * 登录页查询用户
	 *目的：1、查询用户的手机号，获取验证码
	 * @param puv
	 * @return
	 */

	@RequestMapping("/user_login_show")
	@ResponseBody
	public Map<String, Object> user_login_show(HttpServletRequest request, PmsUserVo puv){

		Map<String, Object> map = (Map<String, Object>) userServiceBean.findUser(request, puv);

		return map;
	}

}
